package tp.pr4.gui;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import tp.pr4.RobotEngine;
import tp.pr4.Rotation;
import tp.pr4.gui.listeners.MouseClickListener;
import tp.pr4.instructions.DropInstruction;
import tp.pr4.instructions.MoveInstruction;
import tp.pr4.instructions.OperateInstruction;
import tp.pr4.instructions.PickInstruction;
import tp.pr4.instructions.RedoInstruction;
import tp.pr4.instructions.TurnInstruction;
import tp.pr4.instructions.UndoInstruction;

public class InstructionsPanel extends JPanel {
	
	/**
	 * Creates and adds all the controls to the Instructions Panel
	 * @param r Robot to link commands to
	 */
	public InstructionsPanel(final RobotEngine r, final JTable table) {
		this.setBorder(new TitledBorder("Instructions"));
		
		this.setLayout(new GridLayout(5,2));
		this.add(new JButton("MOVE") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						r.communicateRobot(new MoveInstruction());
					}
				});
			}
		});
		this.add(new JButton("QUIT") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if (JOptionPane.showConfirmDialog(getTopLevelAncestor(), "¿De verdad deseas salir?", "WALL·E says", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
							System.exit(0);
					}
				});
			}
		});
		final JComboBox<Rotation> jCombo = new JComboBox<Rotation>();
		jCombo.addItem(Rotation.LEFT);
		jCombo.addItem(Rotation.RIGHT);
		this.add(new JButton("TURN") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						r.communicateRobot(new TurnInstruction(jCombo.getItemAt(jCombo.getSelectedIndex())));
					}
				});
			}
		});
		this.add(jCombo);
		
		final JTextArea jtext = new JTextArea();
		this.add(new JButton("PICK") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						r.communicateRobot(new PickInstruction(jtext.getText()));
					}
				});
			}
		});
		this.add(jtext);
		this.add(new JButton("DROP") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if (table.getSelectedRow() != 0xffffffff)
							r.communicateRobot(new DropInstruction(table.getModel().getValueAt(table.getSelectedRow(), 0).toString()));
					}
				});
			}
		});
		this.add(new JButton("OPERATE") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if (table.getSelectedRow() != 0xffffffff)
							r.communicateRobot(new OperateInstruction(table.getModel().getValueAt(table.getSelectedRow(), 0).toString()));
						
					}
				});
			}
		});
	
		this.add(new JButton("UNDO") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						r.communicateRobot(new UndoInstruction());
					}
				});
			}
		});
		this.add(new JButton("REDO") {
			private static final long serialVersionUID = 1L;
			{
				this.addMouseListener(new MouseClickListener() {
					@Override
					public void mouseClicked(MouseEvent e) {
						r.communicateRobot(new RedoInstruction());
					}
				});
			}
		});
	}
	
	private static final long serialVersionUID = 1L;
}
